/**
 * 
 */
package com.aaron.server;

import java.io.IOException;

import org.tmatesoft.svn.core.SVNException;

import com.aaron.client.SubversionService;
import com.aaron.client.exceptions.*;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * @author aaron
 * 
 */
public class SubversionServiceImpl extends RemoteServiceServlet implements
		SubversionService {
	static final long serialVersionUID = 456789;
	

	public String getTreeXML(String project_name, String auth)throws DatabaseException {
		try{
			SessionServer session_server=new SessionServer();
			if(session_server.confirmAndSync(auth)){
				SubversionServer subversion_server=new SubversionServer();
				String username=session_server.getUsername(auth);
				ProjectServer project_server=new ProjectServer();
				String svn_username=project_server.getSvnUsername(project_name, username);
				String svn_url=project_server.getSvnUrl(project_name, username);
				return subversion_server.getTreeXML(project_name, SubversionServer.getPath(SubversionServer.getUUID(svn_username, svn_url)));
			}
		}
		catch(DatabaseException e){
			System.err.println ("getTreeXML: DatabaseException: "+e.getMessage());
		}
		catch(Exception e){
			System.err.println ("getTreeXML: Exception: "+e.getMessage());
		}
		return "";
	}

	public void update(String project_name, String auth)throws DatabaseException, SVNException  {
		try{
			SessionServer session_server=new SessionServer();
			if(session_server.confirmAndSync(auth)){
				SubversionServer subversion_server=new SubversionServer();
				String username=session_server.getUsername(auth);
				// fetch svn username and svn password from database
				ProjectServer project_server=new ProjectServer();
				String svn_url=project_server.getSvnUrl(project_name, username);
				String svn_username=project_server.getSvnUsername(project_name, username);
				String svn_password=project_server.getSvnPassword(project_name, username);
				//perform the update
				subversion_server.update(svn_url, svn_username, svn_password);
			}
		}
		catch(DatabaseException e){
			System.err.println ("update: DatabaseException: "+e.getMessage());
		}
		catch(Exception e){
			System.err.println ("update: Exception: "+e.getMessage());
		}
	}

	public void commit(String project_name, String message, String auth)
	throws DatabaseException, SVNException {
		try{
			SessionServer session_server=new SessionServer();
			if(session_server.confirmAndSync(auth)){
				SubversionServer subversion_server=new SubversionServer();
				String username=session_server.getUsername(auth);
				// fetch svn username and svn password from database
				ProjectServer project_server=new ProjectServer();
				String svn_url=project_server.getSvnUrl(project_name, username);
				String svn_username=project_server.getSvnUsername(project_name, username);
				String svn_password=project_server.getSvnPassword(project_name, username);
				subversion_server.commit(svn_url, message, svn_username, svn_password);
			}
		}
		catch(DatabaseException e){
			System.err.println ("update: DatabaseException: "+e.getMessage());
		}
		catch(Exception e){
			System.err.println ("update: Exception: "+e.getMessage());
		}
	}

	public void checkout(String project_name, String url, String svn_username, String svn_password, String auth)throws com.aaron.client.exceptions.DatabaseException, com.aaron.client.exceptions.SVNException, com.aaron.client.exceptions.IOException{
		try{
			SessionServer session_server=new SessionServer();
			if(session_server.confirmAndSync(auth)){
				SubversionServer subversion_server=new SubversionServer();
				String username=session_server.getUsername(auth);
				subversion_server.checkout(project_name, url,  username, svn_username, svn_password);
			}
		}
		catch(SVNException e){
			System.err.println("checkout: SVNException: "+e.getMessage()+e.getErrorMessage().getFullMessage());
			throw new com.aaron.client.exceptions.SVNException("Subversion error. Unable to checkout.");
		}
		catch(SecurityException e){
			System.err.println("checkout: SecurityException: "+e.getMessage());
			throw new com.aaron.client.exceptions.SVNException("Subversion security error. Unable to checkout.");
		}
		catch(IOException e){
			System.err.println("checkout: IOException: "+e.getMessage());
			throw new com.aaron.client.exceptions.IOException("Server I/O error. Unable to checkout.");
		}
		catch(DatabaseException e){
			System.err.println("checkout: DatabaseException: "+e.getMessage());
			throw new com.aaron.client.exceptions.DatabaseException("Server database error. Unable to checkout.");
		}
		catch(Exception e){
			System.err.println("checkout: Exception: "+e.getMessage());
			
		}
	}
}
